home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
bit
/
src
/
ulib
/
errmsg.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
182 lines
/************************************************************************
* $Id: errmsg.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
*
*. Copyright(c) 1993,1994 by T.C. Zhao
* All rights reserved.
*.
*
* Error handling routines.
*
* Messages are divided into graphics and non-graphics types.
* For graphical error messages, a user input may be demanded,
* while for non-graphical messages, a string is printed and
* does nothing else.
*
* The graphical output routine must have the following form:
* void (*gmout)(const char *, const char *, const char *, int);
***********************************************************************/
#if !defined(lint) && defined(F_ID)
char *id_errm = "$Id: errmsg.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdarg.h>
#include <errno.h>
#include "ulib.h"
extern int errno; /* system error no */
#define MAXESTR 1024 /* maximum error string len */
/*
* msg_threshold controls amount of message to print
* 0: only error 1: error and warning
* 2: info 3: lots of messages
*/
/************ Local variables ****************************************/
static FILE *errlog; /* where the msg is going */
static ML_t threshold; /* current threshold */
static ML_t req_level; /* requested message level */
static const char *file; /* source file name */
static int lineno; /* line no. in that file */
static int gout; /* if demand graphics */
static int f_l; /* true if func and line output */
static Gmsgout_ gmout;
/******* set up where err is gonna go **************/
void
set_err_msg_fp(FILE * fp)
{
if (fp)
errlog = fp;
}
/***** Message levels ****************************/
void
set_msg_threshold(ML_t mlevel)
{
threshold = mlevel;
}
/***** Graphics output routine ********************/
void
set_err_msg_func(Gmsgout_ errf)
{
gmout = errf;
}
/******* If output filename and line number ******/
void
set_err_msg_brief(int b)
{
f_l = !b;
}
ErrFunc_ efp_; /* global pointer to shut up lint */
/*******************************************************************
* generate two strings that contain where and why an error occured
*********************************************************************/
/* VARARGS2 */
static void
P_errmsg(const char *func, const char *fmt,...)
{
va_list args;
char *where, *why, *pp, line[20];
static char emsg[MAXESTR + 1];
if (!errlog)
errlog = stderr;
/* if there is nothing to do, do nothing ! */
if (req_level >= threshold && (!gout || !gmout))
return;
/******** find out where the error occured *************/
if (f_l)
{
strcpy(line, lineno > 0 ? itoa(lineno) : "?");
where = (func && *func) ?
vstrcat("In ", func, " [", file, " ", line, "]", (char *) 0) :
vstrcat("In ", file, ", ", line, ":", (char *) 0);
}
else
{
line[0] = '\0';
where = (func && *func) ?
vstrcat(func, "-", (char *) 0) :
strdup(" ");
}
/************** and why ********************************/
emsg[0] = '\0';
why = 0;
/* parse the fmt */
if (fmt && *fmt)
{
va_start(args, fmt);
(void) vsprintf(emsg, fmt, args);
va_end(args);
}
/* check if there is any system errors */
if ((pp = errno ? strerror(errno) : 0))
strcat(strcat(emsg, "--"), pp);
why = emsg;
/*
* have gotten the message, where and why, show it
*/
if (req_level < threshold)
fprintf(errlog, "%s %s\n", where, why);
if (gout && gmout)
gmout("Warning:", where, why, 0);
free_vstrcat(where);
/* reset system errors */
errno = 0;
return;
}
/*********************************************************************
* get the line number in file where error occurred. gui indicates
* if graphics output function is to be called
********************************************************************/
ErrFunc_
whereError(int gui, ML_t level, const char *f, int l)
{
file = f;
lineno = l;
req_level = level;
gout = gui;
return P_errmsg;
}
#ifdef TEST
extern void TC_continue(const char *, const char *, const char *, int);
main()
{
set_err_msg_fp(stderr);
Bark("ThisisMain", "error1");
M_err("ThisisMain", "error1");
}
#endif